<template>
  <div :id="id" :class="className" :style="{height:'200px',width:'100%'}" />
</template>

<script>
import * as echarts from "echarts";

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    info: {
      type: Object,
      default: {}
    }
  },
  data() {
    return {
      chart: null,
    }
  },
  watch: {
    info: {
      handler() {
        this.initChart()
      },
      deep: true
    }
  },
  mounted() {
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id))
      this.chart.setOption({
        color: ['#FF4D40', '#128DEB'],
        tooltip: {
          show: false,
          trigger: 'item'
        },
        title: {
          text: '报警统计',
          left: "center",//对齐方式居中
          top: "center",//距离顶部
          textStyle: {//文字配置
            color: "#fff",//文字颜色
            fontSize: 12,//字号
            fontWeight:'normal',
            textAlign: "center",
            align: "center"//对齐方式
          }
        },
        legend: {
          show:false,
          x:'60%',
          y:'center',
          align: 'left',
          orient : 'vertical',
          itemGap:12,
          icon: "circle",
          itemWidth:9,
          textStyle:{
            //在rich中给formatter添加个别字体颜色，此处的abc对应formatter中的abc
            rich:{
              a:{
                fontSize:12,
                color:{},
              },
              b:{
                fontSize:14,
                color:'#fff',
              },
            },
          },
          formatter:(name,value) =>{
            let data = option.series[0].data;
            let faultVal;
            for (let i = 0; i < data.length; i++){
                if(data[i].name == name){
                    faultVal = data[i].value;
                }
                let v = faultVal;
                return '{a|' + 'name' + '} \n\n {b| ' + v + '}'
            }
          }
        },
        series: [
          {
            name: '报警统计',
            type: 'pie',
            center: ['50%', '50%'],
            radius: ['45%', '65%'],
            avoidLabelOverlap: false,
            label: {
            show: false,
            },
            emphasis: {
              label: {
                show: false,
                fontSize: 40,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: [
                { value: this.info.no_alarm || 0, name: '正常设备' },
                { value: this.info.is_alarm || 0, name: '报警设备' }
            ]
          }
        ]
      })
    }
  }
}
</script>
